layui.use(['element','form'], function(){
    var element = layui.element;
    var form = layui.form;


    setTimeout(function (){
        form.render('select');
    }, 1000);

    form.on('select(product)', function(data){
        vm.productId = data.value;

    });
    form.on('select(user)', function(data){
        vm.userId = data.value;
    });

});

var vm = new Vue({
    el: '#app',
    data: {
        productList:[],
        userList:[],
        productId:"",
        userId:"",
        transferSize:""
    },
    created() {
        this.getMyProduct()
        this.getSameDepartmentUser()
    },
    methods: {
        getMyProduct(){
            $.ajax({
                type: "get",
                url: "/copWarehouse/myself",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        vm.productList = result.data;
                        vm.productId = result.data[0].productId;
                    }
                    layui.form.render('select');
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                    }
                }
            });
        },
        getSameDepartmentUser(){
            $.ajax({
                type: "get",
                url: "/authUser/sameDepartment",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: {},
                success: function (result) {
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        vm.userList = result.data;
                        vm.userId = result.data[0].userId;
                    }
                    layui.form.render('select');
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                    }
                }
            });
        },
        add() {
            var tmp = {
                'productId': vm.productId,
                'inUserId': vm.userId,
                'transferNumber': vm.transferSize
            };
            var data = JSON.stringify(tmp)
            $.ajax({
                type: "post",
                url: "/proTransfer/transfer",
                headers: { 'Content-Type': 'application/json;charset=utf-8' },
                dataType: 'json',
                async: true,
                data: data,
                success: function (result) {
                    console.log(result)
                    if (result.code == 500) {
                        layer.msg(result.msg)
                    } else {
                        alert("成功")
                        window.location.href="/product/userWarehouse.html"
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    if(xhr.status==403) {
                        alert("无权限")
                        location.reload([true])
                    }
                }
            });
        }
    }
})